package com.ops.memcache.utility;


import com.ops.uitl.common.Common;
import com.ops.uitl.data.DataMap;
import com.ops.uitl.data.DatasetList;
import com.ops.uitl.data.IDataset;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

public class HttpCall {
  protected static Common common = Common.getInstance();
  protected static Logger log = Logger.getLogger(HttpCall.class);
  public HttpCall() {
  }

  public static IDataset CallService(String url, String charset) throws Exception {
    Object dataset = new DatasetList();
    new DataMap();

    try {
      HttpClient e = new HttpClient();
      GetMethod getMethod = new GetMethod(url);
      getMethod.getParams().setHttpElementCharset(charset);
      getMethod.getParams().setCredentialCharset(charset);
      getMethod.getParams().setContentCharset(charset);
      int statusCode = e.executeMethod(getMethod);
      if(statusCode != 200) {
        log.error("Oops! 出错了: " + getMethod.getStatusLine());
      }

      DataMap data = new DataMap(getMethod.getResponseBodyAsString());
      if(data.getInt("X_RECORDNUM") > 1) {
        dataset = data.toDataset();
      } else {
        ((IDataset)dataset).add(data);
      }

      getMethod.releaseConnection();
    } catch (RuntimeException var7) {
      log.error("Oops! 出错了: " + var7.getMessage());
      var7.printStackTrace();
    }

    return (IDataset)dataset;
  }

  public static void main(String[] args) throws Exception {
    String host = "http://127.0.0.1:7001";
    String key = "ics_productlist_code_0901";
    String a1 = host + "/httptran/CrmService?data={X_TRANS_TYPE=[\"HTTP\"], X_TRANS_CODE=[\"QCS_MemCacheControl\"], MODE=[\"2\"], KEY=[\"" + key + "\"]}";
    String a2 = host + "/httptran/CrmService?data={X_TRANS_TYPE=[\"HTTP\"], X_TRANS_CODE=[\"QCS_MemCacheControl\"], MODE=[\"3\"], KEY=[\"" + key + "\"], VALUE=[\"里全是翁%{}</>~!@\"]}";
    System.out.println("back:" + CallService(a2, "GBK"));
  }
}
